import UIAbility from '@ohos.app.ability.UIAbility';
import hilog from '@ohos.hilog';
import window from '@ohos.window';
import webSocket from '@ohos.net.webSocket';
import FaultLogger from '@ohos.faultLogger';
import useStore from '../../../store/index'
import { baseUrl } from '../../../apis'

export default class EntryAbility extends UIAbility {
  reconnect(curIns?,url?) {
    // 重连的话 先把上一个socket实例进行事件移除
    if (curIns) {
      curIns.off('message')
      curIns.off('error')
      curIns.off('open')
      curIns.off('close')
    }
    let ws = webSocket.createWebSocket()
    this.initSocket(ws,url)
  }


  initSocket(ws,url?){
    let soc = ws || webSocket.createWebSocket() // 如果有传入socket实例就是
    let ping
    // 接受到消息事件
    soc.on('message',(err,value) => {
      console.log('socket message fired')
      console.log(value) // 约定从webscoket传递过来的值是字符串
      const {type,payload} = JSON.parse(value)
      this.context.eventHub.emit(type,payload)
    })
    // 出现错误的事件
    soc.on('error',(err)=>{
      console.log('socket error fired')
      setTimeout(()=>{
        if (ping) clearInterval(ping)
        this.reconnect(false,url)
      },2000)
    })
    // 连接成功
    soc.on('open',()=> {
      console.log('打开socket通讯记录')
      ping = setInterval(()=>{
        // 约定传递给服务器的值都是字符串 所以约定格式是{type:'',payload:''}
        soc.send(JSON.stringify({type:'ping'})) // 保持跟服务器的心跳连接 避免被socket中断通讯
      },3000)
      // 全局订阅发送socket消息
      this.context.eventHub.on('sendmsg',(data)=> soc.send(JSON.stringify(data)))
    })
    // 链接关闭
    soc.on('close',()=> {
      console.log('socket close fired')
      setTimeout(()=>{
        if (ping) clearInterval(ping)
        this.reconnect(false,url)
      },2000)
    })
    // 开始去链接socket服务器
    soc.connect(url,(err,value)=> {
      if(!err) {
        console.log('connect ok')
      } else {
        console.log('与服务器链接失败')
        setTimeout(()=>{
          this.reconnect(false,url)
        },2000)
      }
    })
  }

  onCreate(want, launchParam) {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
  //   初始化我们的socket通讯
    this.context.eventHub.on('startSocket',(url)=>{
      this.initSocket(false,url )
    })

    useStore.initStore(this.context)
    // 创建一个全局的环境变量
    Environment.EnvProp('baseHost',baseUrl)
  }

  onDestroy() {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
  }

  onWindowStageCreate(windowStage: window.WindowStage) {
    // Main window is created, set main page for this ability
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');

    windowStage.loadContent('pages/passenger/home', (err, data) => {
      if (err.code) {
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
        return;
      }
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
    });
  }

  onWindowStageDestroy() {
    // Main window is destroyed, release UI related resources
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
  }

  onForeground() {
    // Ability has brought to foreground
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
  }

  onBackground() {
    // Ability has back to background
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
  }
}
